package com.example.demodb.service;

import com.example.demodb.entity.Song;
import com.example.demodb.entity.SongSong;
import com.example.demodb.repository.ISongRepository;
import com.example.demodb.repository.ISongSongRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SongService { //service可以抽象成接口
    @Resource
    private ISongRepository songRepository;

    @Resource
    private ISongSongRepository songSongRepository;
    public Song update(Song song){
        Song find_song = findSongById(song.getId());
        if(find_song!=null){
            find_song.setSongUrl(song.getSongUrl());
            find_song.setImageUrl(song.getImageUrl());
            find_song.setLrc(song.getLrc());
            find_song.setStyle(song.getStyle());
            return songRepository.save(find_song);
        }else{
            return null;
        }
    }
    public void delete(String id){
         songRepository.deleteById(id);
    }
    public List<Song> findAll(){
        return songRepository.findAll();
    }
    public Song add(Song song){
        Song find_song = findSongById(song.getId());
        if(find_song!=null) {
            return null;
        }
        return songRepository.save(song);
    }

    public Song findSongById(String songId){
        return songRepository.findSongById(songId);
    }

    public SongSong addSongSong(String songId1, String songId2, Double sim){
        //避免 歌1 和 歌1相似度=1的情况填入数据库中
        if(!Objects.equals(songId1, songId2)){
            Song song1 = songRepository.findSongById(songId1);
            Song song2 = songRepository.findSongById(songId2);
            SongSong song1Song2= songSongRepository.findSongSongBySong1AndSong2(song1, song2);
            //避免出现 1,2 = 0.6  & 2,1 = 0.3 这种重复
            if(song1Song2== null){
                song1Song2= songSongRepository.findSongSongBySong1AndSong2(song2, song1);
                if(song1Song2== null){
                    song1Song2= songSongRepository.save(song1.addSongSong(song2, sim));
                }else{
                    song1Song2= songSongRepository.save(song1.changeSim(song1Song2, sim));
                }
            }else{
                song1Song2= songSongRepository.save(song1.changeSim(song1Song2, sim));
            }
//        if(song1Song2== null){
//            song1Song2= songSongRepository.save(song1.addSongSong(song2, sim));
//        }else{
//            song1Song2= songSongRepository.save(song1.changeSim(song1Song2, sim));
//        }
            return song1Song2;
        }else {
            return null;
        }

    }

    public SongSong addSongSongByTitle(String songTitle1, String songTitle2, Double sim){
        //避免 歌1 和 歌1相似度=1的情况填入数据库中
        if(!Objects.equals(songTitle1, songTitle2)){
            Song song1 = songRepository.findFirstByTitle(songTitle1);
            Song song2 = songRepository.findFirstByTitle(songTitle2);
            SongSong song1Song2= songSongRepository.findSongSongBySong1AndSong2(song1, song2);
            //避免出现 1,2 = 0.6  & 2,1 = 0.3 这种重复
            if(song1Song2== null){
                song1Song2= songSongRepository.findSongSongBySong1AndSong2(song2, song1);
                if(song1Song2== null){
                    song1Song2= songSongRepository.save(song1.addSongSong(song2, sim));
                }else{
                    song1Song2= songSongRepository.save(song1.changeSim(song1Song2, sim));
                }
            }else{
                song1Song2= songSongRepository.save(song1.changeSim(song1Song2, sim));
            }
//        if(song1Song2== null){
//            song1Song2= songSongRepository.save(song1.addSongSong(song2, sim));
//        }else{
//            song1Song2= songSongRepository.save(song1.changeSim(song1Song2, sim));
//        }
            return song1Song2;
        }else {
            return null;
        }

    }
    public SongSong findSong1Song2Sim(String songId1, String songId2) {
        Song song1 = songRepository.findSongById(songId1);
        Song song2 = songRepository.findSongById(songId2);
        return songSongRepository.findSongSongBySong1AndSong2(song1, song2);
    }

    public List<List<SongSong>> findSimSongsByTitle(String title) {
        Song song = songRepository.findFirstByTitle(title);
        List<List<SongSong>> l = new ArrayList<>();
        List<SongSong> song1Song2List = songSongRepository.findAllSongSongBySong1(song);
        List<SongSong> song2Song1List = songSongRepository.findAllSongSongBySong2(song);
        l.add(song1Song2List);
        l.add(song2Song1List);
        return l;
    }

    public List<Song> randomGenerateSongsByNum(Integer num){
        return songRepository.randomGenerate(num);
    }

    public List<Song> findSongsByStyle(String style) {
        return songRepository.findSongsByStyle(style);
    }

    public Song findSongsByTitle(String title) {
        return songRepository.findFirstByTitle(title);

    }

    public List<Song> findAllBySearchContains(String search) {
        List<Song> songList = new ArrayList<>();
        if(!Objects.equals(search, "")){
            songList.addAll(songRepository.findAllByArtistContains(search));
            songList.addAll(songRepository.findAllByTitleContains(search));
            for (int i = 0; i < songList.size(); i++) { //做去重操作, 防止  singer, songname中含有相同的字段, 重复显示
                for (int j = 0; j < songList.size(); j++) {
                    if(i!=j&&songList.get(i)==songList.get(j)) {
                        songList.remove(songList.get(j));
                    }
                }
            }
        }

        return songList;

    }
}
